home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayFile.frm < prev    next >
Text File  |  1999-05-27  |  9KB  |  316 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayFile 
  4.    Caption         =   "PlayFile"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.TextBox txtNumFrames 
  15.       Height          =   285
  16.       Left            =   1560
  17.       TabIndex        =   9
  18.       Text            =   "100"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.OptionButton optRunType 
  23.       Caption         =   "Looping"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   360
  27.       TabIndex        =   7
  28.       Top             =   1560
  29.       Width           =   1095
  30.    End
  31.    Begin VB.OptionButton optRunType 
  32.       Caption         =   "Reversing"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   360
  36.       TabIndex        =   6
  37.       Top             =   1200
  38.       Width           =   1095
  39.    End
  40.    Begin VB.OptionButton optRunType 
  41.       Caption         =   "One time"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   360
  45.       TabIndex        =   5
  46.       Top             =   840
  47.       Value           =   -1  'True
  48.       Width           =   1095
  49.    End
  50.    Begin VB.TextBox txtFramesPerSecond 
  51.       Height          =   285
  52.       Left            =   1560
  53.       TabIndex        =   4
  54.       Text            =   "20"
  55.       Top             =   480
  56.       Width           =   375
  57.    End
  58.    Begin VB.CommandButton cmdStart 
  59.       Caption         =   "Start"
  60.       Default         =   -1  'True
  61.       Enabled         =   0   'False
  62.       Height          =   375
  63.       Left            =   600
  64.       TabIndex        =   1
  65.       Top             =   2040
  66.       Width           =   855
  67.    End
  68.    Begin VB.PictureBox picCanvas 
  69.       Height          =   3810
  70.       Left            =   2040
  71.       ScaleHeight     =   250
  72.       ScaleMode       =   3  'Pixel
  73.       ScaleWidth      =   250
  74.       TabIndex        =   0
  75.       Top             =   0
  76.       Width           =   3810
  77.    End
  78.    Begin MSComDlg.CommonDialog dlgOpenFile 
  79.       Left            =   1560
  80.       Top             =   960
  81.       _ExtentX        =   847
  82.       _ExtentY        =   847
  83.       _Version        =   393216
  84.       CancelError     =   -1  'True
  85.    End
  86.    Begin VB.Label Label2 
  87.       Caption         =   "Frames to load:"
  88.       Height          =   255
  89.       Left            =   120
  90.       TabIndex        =   8
  91.       Top             =   120
  92.       Width           =   1455
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "Frames per second:"
  96.       Height          =   255
  97.       Index           =   1
  98.       Left            =   120
  99.       TabIndex        =   3
  100.       Top             =   480
  101.       Width           =   1455
  102.    End
  103.    Begin VB.Label lblResults 
  104.       Height          =   615
  105.       Left            =   120
  106.       TabIndex        =   2
  107.       Top             =   2640
  108.       Width           =   1815
  109.    End
  110.    Begin VB.Menu mnuFile 
  111.       Caption         =   "&File"
  112.       Begin VB.Menu mnuFileOpen 
  113.          Caption         =   "&Open..."
  114.          Shortcut        =   ^O
  115.       End
  116.    End
  117. End
  118. Attribute VB_Name = "frmPlayFile"
  119. Attribute VB_GlobalNameSpace = False
  120. Attribute VB_Creatable = False
  121. Attribute VB_PredeclaredId = True
  122. Attribute VB_Exposed = False
  123. Option Explicit
  124.  
  125. Private NumImages As Integer
  126. Private MaxImage As Integer
  127. Private Playing As Boolean
  128.  
  129. Private FileName As String
  130. Private NumPlayed As Long
  131. ' See how many images are available.
  132. Private Sub LoadImages(file_name As String)
  133. Dim base As String
  134. Dim i As Integer
  135.  
  136.     ' Remove the "0.bmp" from the file name.
  137.     FileName = Left$(file_name, Len(file_name) - 5)
  138.  
  139.     ' See how many frames the user wants to load.
  140.     If Not IsNumeric(txtNumFrames.Text) Then _
  141.         txtNumFrames.Text = Format$(10)
  142.     NumImages = CInt(txtNumFrames.Text)
  143.  
  144.     ' See which files exist.
  145.     i = 0
  146.     Do While i < NumImages
  147.         lblResults.Caption = Format$(i + 1)
  148.         lblResults.Refresh
  149.         If Dir$(FileName & Format$(i) & ".bmp") = "" Then Exit Do
  150.         i = i + 1
  151.     Loop
  152.     NumImages = i
  153.  
  154.     ' Load the first frame.
  155.     picCanvas.AutoSize = True
  156.     picCanvas.Picture = LoadPicture(FileName & "0.bmp")
  157.     picCanvas.AutoSize = False
  158.  
  159.     lblResults.Caption = ""
  160.     txtNumFrames.Text = Format$(NumImages)
  161. End Sub
  162.  
  163. ' Run the animation until Playing is false.
  164. Private Sub PlayImages()
  165. Dim ms_per_frame As Integer
  166. Dim start_time As Long
  167. Dim stop_time As Long
  168.  
  169.     ' See how long it should be between frames.
  170.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  171.         txtFramesPerSecond.Text = "20"
  172.     ms_per_frame = 1000 / CInt(txtFramesPerSecond.Text)
  173.  
  174.     ' Start the appropriate animation.
  175.     NumPlayed = 0
  176.     start_time = GetTickCount
  177.     If optRunType(0).Value Then
  178.         PlayImagesOnce ms_per_frame
  179.     ElseIf optRunType(1).Value Then
  180.         PlayImagesBackAndForth ms_per_frame
  181.     Else
  182.         PlayImagesLooping ms_per_frame
  183.     End If
  184.  
  185.     ' Display results.
  186.     stop_time = GetTickCount
  187.     lblResults.Caption = _
  188.         Format$(NumPlayed) & " frames/" & _
  189.         Format$((stop_time - start_time) / 1000#, "0.00") & _
  190.         " sec" & vbCrLf & vbCrLf & _
  191.         Format$(CSng(NumPlayed) / ((stop_time - start_time) / 1000#), "0.00") & _
  192.         " frames/sec"
  193. End Sub
  194. ' Run the animation forward and backward until
  195. ' Playing is False.
  196. Private Sub PlayImagesBackAndForth(ByVal ms_per_frame As Integer)
  197.     ' Start the animation.
  198.     Do While Playing
  199.         PlayImagesOnce ms_per_frame
  200.         If Not Playing Then Exit Do
  201.         PlayImagesReversed ms_per_frame
  202.     Loop
  203. End Sub
  204. ' Run the animation until Playing is false.
  205. Private Sub PlayImagesLooping(ByVal ms_per_frame As Integer)
  206.     ' Start the animation.
  207.     Do While Playing
  208.         PlayImagesOnce ms_per_frame
  209.     Loop
  210. End Sub
  211. ' Run the animation once or until Playing is False.
  212. Private Sub PlayImagesOnce(ByVal ms_per_frame As Integer)
  213. Dim i As Integer
  214. Dim next_time As Long
  215.  
  216.     ' Get the current time.
  217.     next_time = GetTickCount
  218.  
  219.     ' Start the animation.
  220.     For i = 0 To NumImages - 1
  221.         ' Display the next frame.
  222.         picCanvas.Picture = _
  223.             LoadPicture(FileName & Format$(i) & ".bmp")
  224.         NumPlayed = NumPlayed + 1
  225.  
  226.         ' Wait till we should display the next frame.
  227.         next_time = next_time + ms_per_frame
  228.         WaitTill next_time
  229.  
  230.         If Not Playing Then Exit For
  231.     Next i
  232. End Sub
  233. ' Run the animation reversed once or until Playing
  234. ' is False.
  235. Private Sub PlayImagesReversed(ByVal ms_per_frame As Integer)
  236. Dim i As Integer
  237. Dim next_time As Long
  238.  
  239.     ' Get the current time.
  240.     next_time = GetTickCount
  241.  
  242.     ' Start the animation.
  243.     For i = NumImages - 1 To 0 Step -1
  244.         ' Display the next frame.
  245.         picCanvas.Picture = _
  246.             LoadPicture(FileName & Format$(i) & ".bmp")
  247.         NumPlayed = NumPlayed + 1
  248.  
  249.         ' Wait till we should display the next frame.
  250.         next_time = next_time + ms_per_frame
  251.         WaitTill next_time
  252.  
  253.         If Not Playing Then Exit For
  254.     Next i
  255. End Sub
  256.  
  257. ' Start or stop playing.
  258. Private Sub CmdStart_Click()
  259.     If Playing Then
  260.         Playing = False
  261.         cmdStart.Caption = "Stopped"
  262.         cmdStart.Enabled = False
  263.     Else
  264.         cmdStart.Caption = "Stop"
  265.         lblResults.Caption = ""
  266.         DoEvents
  267.         Playing = True
  268.         PlayImages
  269.         Playing = False
  270.         cmdStart.Caption = "Start"
  271.         cmdStart.Enabled = True
  272.     End If
  273. End Sub
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. Private Sub Form_Load()
  281.     dlgOpenFile.InitDir = App.Path
  282. End Sub
  283.  
  284. ' Load new image files.
  285. Private Sub mnuFileOpen_Click()
  286. Dim file_name As String
  287.  
  288.     ' Let the user select a file.
  289.     On Error Resume Next
  290.     dlgOpenFile.FileName = "*_0.BMP"
  291.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  292.     dlgOpenFile.ShowOpen
  293.     If Err.Number = cdlCancel Then
  294.         Exit Sub
  295.     ElseIf Err.Number <> 0 Then
  296.         Beep
  297.         MsgBox "Error selecting file.", , vbExclamation
  298.         Exit Sub
  299.     End If
  300.     On Error GoTo 0
  301.  
  302.     Screen.MousePointer = vbHourglass
  303.     DoEvents
  304.  
  305.     file_name = Trim$(dlgOpenFile.FileName)
  306.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  307.         - Len(dlgOpenFile.FileTitle) - 1)
  308.     Caption = "PlayFile [" & dlgOpenFile.FileTitle & "]"
  309.  
  310.     ' See how many images are available.
  311.     LoadImages file_name
  312.  
  313.     cmdStart.Enabled = True
  314.     Screen.MousePointer = vbDefault
  315. End Sub
  316.